<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!--返回错误信息-->
<?php echo validation_errors(); ?>
<!DOCTYPE html>
<html lang="en">

<head>
    <?php
    $title = [
        'title' => '详情招标项目',
    ];
    $this->load->view('/admin/common/header', $title);
    ?>
</head>

<body data-type="widgets">
<script src="/assets/js/theme.js"></script>
<div class="am-g tpl-g">

    <!--    头部   侧边菜单-->
    <?php $this->load->view('/admin/common/bodyLeft'); ?>
    <script type="text/javascript">
    	$(".js-sidebar-nav>li:nth-child(12)>ul").show(80);
    	$(".js-sidebar-nav>li:nth-child(12)>a>span").addClass("sidebar-nav-sub-ico-rotate");
    	$(".js-sidebar-nav>li:nth-child(12)>ul>li:nth-child(1)>a").addClass("active");
    </script>
    <!-- 内容区域 -->
    <div class="tpl-content-wrapper">
			<div class="row-content am-cf">
				<div class="row">
					<div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
						<div class="widget am-cf">
							<div class="widget-head am-cf">
								<div class="widget-title  am-cf">
									<ol class="am-breadcrumb am-breadcrumb-slash">
										<li><a href="/admin/bidding/index?page=1">招投标管理</a></li>
                    <li class="am-active">详情招标项目</li>
									</ol>
								</div>
							</div>
							<form class="am-text-sm am-form" id="submitForm">
								<div class="widget-body  am-fr">
									<div class="tpl-table-black-operation">
										<a href="javascript:void(0);" class="tpl-table-black-operation-del">
											基本信息
										</a>
									</div>

									<div style="margin-top: 20px">
										<div class="am-g">
											<!-- Row start -->
											<div class="am-u-sm-12">
												<div class="am-g-fixed am-cf">
													<div class="am-u-md-6">														
														<div class="am-form-group am-u-sm-12">
																<label class="am-u-md-2 am-md-text-right am-padding-left-0 must" for="projectid">项目编号</label>
																<div class="am-u-sm-10">
																	<input type="text" name="code" value="<?php echo $list['code'] ?>" id="projectid" readonly="readonly">
																</div>
																
														</div>

														<div class="am-form-group am-u-sm-12">
																<label class="am-u-md-2 am-md-text-right am-padding-left-0 must" for="name">项目名称</label>
																<div class="am-u-sm-10">
																	<input  type="text" name="name" value="<?php echo $list['name'] ?>" id="name" placeholder="请填写项目名称">
																</div>
																
																
														</div>

														<div class="am-form-group am-u-sm-12">
																<label class="am-u-md-2 am-md-text-right am-padding-left-0 must" for="time">发布时间</label>
																<div class="am-u-sm-10">
																	<input type="date" class="am-u-md-10 form-control" name="createTime" value="<?php echo date('Y-m-d',$list['createTime']) ?>" id="time">
																</div>
																
																
														</div>
														<div class="am-form-group  am-u-md-12">
															<label class="am-u-md-2 must">所在地区</label>														
														    <div id="distpicker" class="am-u-md-10">
														        <select class="form-control" id="province" data-am-selected="{btnWidth: '32%',maxHeight: 200}"></select>
														        <select class="form-control" id="city" data-am-selected="{btnWidth: '32%',maxHeight: 200}"></select>
														        <select class="form-control" id="district" data-am-selected="{btnWidth: '32%',maxHeight: 200}"></select>
														    </div>
														</div>
													</div>

													<div class="am-u-md-6">
                                                        <?php
                                                        if(isset($_GET['needCode'])){
                                                            echo '<div class="am-form-group am-u-sm-12">
																<label class="am-u-md-2 am-md-text-right am-padding-left-0 must" for="xqdid">需求单号</label>
																<div class="am-u-sm-10">
																<input  type="text"  name="needCode" value="'.$list['needCode'].'" id="xqdid" readonly>
																</div>
																
																
														</div>';
                                                        }
                                                        ?>

														<div class="am-form-group am-u-sm-12">
																<label class="am-u-md-2 am-md-text-right am-padding-left-0 must" for="timelimit">有效时间</label>
																<div class="am-u-sm-10">
																	<input type="number" class="am-u-md-10 form-control" name="validTime" value="<?php echo $list['validTime'] ?>" id="timelimit">
																</div>
																
																
														</div>
	
												    <div class="am-form-group am-u-md-12">
												      <label class="am-u-sm-2 am-form-label" for="projectImg">项目附件</label>
												      <div class="am-inline-block am-form-file am-u-sm-10">
												        <button type="button" class="am-btn am-btn-default am-btn-sm">
												        <i class="am-icon-cloud-upload"></i> 选择要上传的文件</button>
												      	<input type="file" id="projectImg" name="projectImg">
							      						<span class="file-show"><?php echo $list['projectImgOldName']?></span>

												      </div>
												    </div>
													</div>
													
													
													<div class="am-form-group  am-u-md-12">
														<div class="am-g">
															<label class="am-u-md-2">预选供应商</label>
															<div class="am-u-md-10">
																<button type="button" id="btn-select" class="am-btn am-btn-primary tpl-btn-bg-color-success" data-am-modal="{target: '#changeCommodity', closeViaDimmer: 0, width: 800, height: 500}">选择供应商</button>																
																<table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black " id="example-r" >
																  <thead>
																	  <tr>
																	    <th>供应商编号</th>
																	    <th>供应商名称</th>
																	    <th>供应商地址</th>
																	    <th>操作</th>
																	  </tr>
																  </thead>
																  <tbody>
                                                                  <?php
                                                                        if(isset($list['supplierInfo'])){
                                                                            foreach ($list['supplierInfo'] as $k=>$v){
                                                                                echo ' <tr>
                                                                            <td>'.$v['id'].'</td>
                                                                            <td>'.$v['name'].'</td>
                                                                            <td>'.$v['address'].'</td>
                                                                            <td><div class="tpl-table-black-operation"><a href="javascript:void(0)" class="tpl-table-black-operation-del js-delete"><i class="am-icon-trash"></i> 删除</a></div></td>
                                                                        </tr>';
                                                                            }

                                                                        }

                                                                  ?>

                                                                  </tbody>
																</table>																
															</div>
														</div>
													</div>											
													
													<div class="am-form-group  am-u-md-12">
														<div class="am-g">
															<label class="am-u-md-2">项目简介</label>
															<div class="am-u-md-10">
																<script id="editor" type="text/plain" style="width: 100%; height: 300px;"></script>
															</div>	
														</div>
													</div>														
												</div>
											</div>
										</div>										
									</div>
									<div class="tpl-table-black-operation">
										<hr />
										<a href="javascript:void(0);" class="tpl-table-black-operation-del">其他信息</a>
									</div>
									<br />
									<div class="am-g-fixed">
									<div class="am-u-md-6">
										<div class="am-form-group am-u-md-12 am-cf">
										    <label for="doc-ipt-3" class="am-u-sm-2 am-form-label">需要的资质</label>
										    <div class="am-u-sm-10">
										      	<div class="am-checkbox">
											      	<label><input type="checkbox" name="needData" value="营业执照" />营业执照</label>	
										      	</div>	
										    </div>
										</div>
										<div class="am-form-group  am-u-md-12">
										    <label for="contacts" class="am-u-sm-2 am-form-label must">联系人</label>
										    <div class=" am-u-md-10">
										    	<input type="text" id="contacts" name="contactMan" class="form-control" value="<?php echo $list['contactMan'] ?>">
										    </div>
										    
										</div>
									</div>
									<div class="am-u-md-6">
										<div class="am-form-group  am-u-md-12">
										    <label for="bzmoney" class="am-u-sm-2 am-form-label must">需要交纳保证金</label>
										    <div class="am-u-md-10 am-input-group">
										    	<input type="number" id="bzmoney" name="needAmount" value="<?php echo $list['needAmount'] ?>"  class="am-form-field">
										    	<span class="am-input-group-label">元</span>
										    </div>
										    
									    </div>
									    <div class="am-form-group  am-u-md-12">
										    <label for="phone" class="am-u-sm-2 am-form-label must">联系电话</label>
										    <div class="am-u-md-10">
										    	<input type="tel" id="phone" name="phone" value="<?php echo $list['phone'] ?>" class="form-control">
										    </div>
										    
										</div>
									</div>
									</div>
								  
									<div class="tpl-table-black-operation  am-u-md-12">
										<hr />
										<a href="javascript:void(0);" class="tpl-table-black-operation-del">
											文件上传
										</a>
									</div>
									<br />

                                    <?php
                                        if($list['isPay']){
                                                                        echo '<div class="am-form-group am-u-md-12 am-g-fixed">
							    	<br />
							      <label class="am-u-sm-2 am-form-label must" for="biddingFile">下载标书文件</label>
										<div class="am-inline-block am-form-file">
										 <a href="'.$list['biddingFile'].'" download="标书文件">'.$list['biddingFileOldName'].'</a>
										</div>
							    </div>';
                                                                    }else{
                                            echo '';
                                        }
                                      ?>

							    <div class="am-form-group am-u-md-12 am-g-fixed">
							      <label class="am-u-sm-2 am-form-label must" for="procurementImg">采购清单</label>
							      <div class="am-inline-block am-form-file">
							        <button type="button" class="am-btn am-btn-default am-btn-sm">
							        <i class="am-icon-cloud-upload"></i> 选择要上传的文件</button>
							      	<input type="file" id="procurementImg" name="procurementImg">
							      	<span class="file-show"><?php echo $list['procurementOldName']?></span>

							      </div>
							    </div>									
								
									<div class="am-u-sm-12 btn-box">
                                         <?php
                                             if($list['status'] == 0){
                                                 echo '	<input type="submit" value="暂存" name="action" class="am-btn am-btn-primary tpl-btn-bg-color-success am-u-sm-centered commit" style="width:220px;margin: 20px;">
										<input type="submit" value="提交" name="action" class="am-btn am-btn-primary tpl-btn-bg-color-success am-u-sm-centered commit" style="width:220px;margin: 20px;">';
                                             }else{
                                                 echo '<button onclick="back()" type="button" class="am-btn am-btn-primary tpl-btn-bg-color-success" style="width:220px">取消</button>';
                                             }
                                         ?>

									</div>
							</div>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>

<!-- 选择供应商弹出框 -->
<div class="am-modal am-modal-no-btn" tabindex="-1" id="changeCommodity" style="">
  <div class="am-modal-dialog">
    <div class="am-modal-hd">选择供应商
      <a href="javascript: void(0)" class="am-close am-close-spin" data-am-modal-close>&times;</a>
    </div>
    
    <div class="am-modal-bd r_pt">
      <div>
        <form class="am-form am-form-horizontal">
          <div class="am-form-group search-box">
            <div class="am-u-sm-8 search-input">
              <input type="text" id="" placeholder="请输入商品名称、商品编号查询。">
            </div>
            <div class="am-u-sm-2" >
              <button type="button" class="am-btn am-btn-success">查询</button>
            </div>
            <div class="am-u-sm-2">
              <button type="button" class="am-btn am-btn-primary" id="addCommodity" data-am-modal-close>确认</button>
            </div>
          </div>
        </form>
        <div class="am-u-sm-12">
          <table class="am-table am-table-bordered am-table-radius am-table-striped am-table-hover">
            <thead>
              <tr>
                <th><input type="checkbox" class="check-all" name=""></th>
                <th>序号</th>
                <th>供应商编号</th>
                <th>供应商名称</th>
                <th>地址</th>
              </tr>
            </thead>
            <tbody class="tr-box">
							<tr>
								<td><input type="checkbox" class="check-one" name=""></td>
								<td>01</td>
								<td>gys2394872495</td>
								<td>公司名</td>
								<td>杭州</td>
							</tr>
            </tbody>
          </table>
        </div>

        <div class="pagination-box">
          <ul class="am-pagination" id="pagination">

          </ul>
        </div>
      </div>
    </div>
</div>

<script src="/assets/js/amazeui.min.js"></script>
<script src="/assets/js/amazeui.datatables.min.js"></script>
<script src="/assets/js/dataTables.responsive.min.js"></script>
<script src="/assets/js/app.js"></script>
<script src="/assets/js/distpicker.data.js"></script>
<script src="/assets/js/distpicker.js"></script>
<script src="/assets/js/UEditor/ueditor.config.js"></script>
<script src="/assets/js/UEditor/ueditor.all.min.js"> </script>
<script src="/assets/js/zdy_form.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
	var totalPage = 0;
	var arr = [];
	var arrSelect = [];
	var supplierInfo = [];
	$("#example-r tbody tr").each(function() {
		var str = $(this).find("td").eq(0).text();
		supplierInfo.push(str);
	});
	console.log(supplierInfo);

	$(function() {

		fileShow("#projectImg");
		fileShow("#biddingFile");
		fileShow("#procurementImg");

		function fileShow(id){
			$(id).change(function (event) {
	            var filePath = $(this).val();
	            if(filePath){
	                var arr = filePath.split('\\');
	                var fileName = arr[arr.length-1];
	                $(this).parent().find(".file-show").html(fileName);
	            }else{
	                $(this).parent().find(".file-show").html("");
	                return false;
	            }
	            
	        });
		}

		var strAddress = "<?php echo $list['address'] ?>";
		var arrAddress = strAddress.split("/");
		$("#distpicker").distpicker({
		  province: arrAddress[0],
		  city: arrAddress[1],
		  district: arrAddress[2]
		});
	  var ue = UE.getEditor('editor');
	  ue.ready(function() {
	    ue.setContent('<?php echo $list['synopsis'] ?>');
		});
	  	
	  
	  // 点击 "选择供应商"按钮
	  $("#btn-select").click(function() {
			postDataByPage(1);
	  });
	  

	  //已选择供应商
		var arrSelect = [];
		$("#example-r>tbody>tr").each(function(){
			var obj = {};
			obj.id = $(this).find("td").eq(0).text();
			obj.name = $(this).find("td").eq(1).text();
			obj.address = $(this).find("td").eq(2).text();
			arrSelect.push(obj);
		});



	  function postDataByPage(p) {
	  	$.ajax({
	  		type:"post",
	  		url:"/api/supplier/supplierList",
	  		async:true,
	  		data: {page: p},
	  		dataType: "json",
	  		success: function(data) { 
	  			arr = data["list"]; 
	  			// 数据列表			
					changeDomOfPopoverList();
					
			  	// 分页列表
	  			totalPage = Math.ceil(data["totalNum"] / data["pageNum"]);	
					changeDomOfPage(p)
	  		}
	  	});	  	
	  }
	  
	  function changeDomOfPopoverList() {
	  	var html = '';
	  	for (var i = 0; i < arr.length; i++) {
	  		var obj = arr[i];
				var isCheck = "";
	  		for (var j = 0; j < arrSelect.length; j++) {
	  			if (arrSelect[j].id == obj.id) {
	  				isCheck = "checked";
	  				break;
	  			}
	  		}			  		
		  	html += '<tr>';
				html += '	<td><input type="checkbox" class="check-one" data-id=' + obj.id + ' ' + isCheck + '></td>';
				html += '	<td>' + obj.id + '</td>';
				html += '	<td>' + obj.code + '</td>';
				html += '	<td>' + obj.name + '</td>';
				html += '	<td>' + obj.address + '</td>';
				html += '</tr>';		  		
	  	}
	  	$(".tr-box").html(html);	  	
	  	
			isCheckedOfUpper();
	  }
	  
	  // 判断全选框的状态
	  function isCheckedOfUpper() {
	  	isCheck = $(".check-one:checked").size() == arr.length;
	  	$(".check-all").prop("checked", isCheck);	  	
	  }
	  
	  function changeDomOfPage(p) {
			var html = '';
			html += '<li><a href="javascript:void(0)" class="prev-page">&laquo;</a></li>';
			for (var i = 1; i < totalPage + 1; i++) {
				strClass = i == p ? "am-active" : "";	  				
				html += '<li class="' + strClass + '"><a href="javascript:void(0)">' + i + '</a></li>';
			}					
      html += '<li><a href="javascript:void(0)" class="next-page">&raquo;</a></li>';
      $("#pagination").html(html);	  	
	  }
	  
    //全选反选
    $(document).on("click", ".check-all", function() {
    	var isCheck = this.checked;
    	$(".check-one").each(function() {
    		if (isCheck && !this.checked) {
    			// 全选, 单个未选中时
    			$(this).prop("checked", isCheck);
    			var i = $(this).parents("tr").index();
    			arrSelect.push(arr[i]);    			    			
    		} else if (!isCheck && this.checked) {
    			// 全不选, 单个选中时
    			$(this).prop("checked", isCheck);
    			var i = $(this).parents("tr").index();
    			arrSelect.splice(arrSelect.indexOf(arr[i]), 1);			
    		}   		
    	});      	
    });
    
    $(document).on("click", ".check-one", function() {
    	isCheckedOfUpper();
    	
    	var i = $(this).parents("tr").index();
			if (this.checked) {				
				arrSelect.push(arr[i]);
			} else {
				for (var j = 0; j < arrSelect.length; j++) {
					if (arrSelect[j].id == $(this).attr("data-id")) {
						arrSelect.splice(j, 1);
						break;
					}
				}				
			}
    });
        	  
	  // 选择供应商 弹框中, "确认"按钮
	  $("#addCommodity").click(changeDomOfgysList);
	  
	  // 加载 表单中预选供应商列表
	  function changeDomOfgysList() {
	  	// 当没有数据时隐藏列表
	  	if (arrSelect.length == 0) {
	  		$("#example-r").hide();
	  	} else {
	  		$("#example-r").show();
		  	var html = '';
				for (var i = 0; i < arrSelect.length; i++) {
					var obj = arrSelect[i];
					html += '<tr>';
			    html += '	<td>' + obj.id + '</td>';
			    html += '  <td>' + obj.name + '</td>';
			    html += '  <td>' + obj.address + '</td>';
			    html += '  <td>';
			    html += '    <div class="tpl-table-black-operation">';
			    html += '      <a href="javascript:void(0)" class="tpl-table-black-operation-del js-delete">';
			    html += '        <i class="am-icon-trash"></i> 删除';
			    html += '      </a>';
			    html += '    </div>';
			    html += '  </td>';
			    html += '</tr>';
				}
				$("#example-r tbody").html(html);		  		
	  	}	  		  	  	
	  }
	  
	  // 选择供应商 弹框中, 上下页按钮
	  $(document).on("click", ".prev-page", function() {
	  	if ($(this).parent().next().prop("class") == "am-active") {
	  		return;
	  	}
	  	var pageActive = $("#pagination>.am-active>a").text();
	  	pageActive--;
	  	postDataByPage(pageActive);
	  });	  
	  
	  $(document).on("click", ".next-page", function() {
	  	if ($(this).parent().prev().hasClass("am-active")) {
	  		return;
	  	}	  	
	  	var pageActive = $("#pagination>.am-active>a").text();
	  	pageActive++;
	  	postDataByPage(pageActive);
	  });	
	  // 选择供应商 弹框中, 分页按钮  
	  $(document).on("click", "#pagination>li>a", function() {
	  	if ($(this).hasClass("prev-page") || $(this).hasClass("next-page") || $(this).parent().hasClass("am-active")) {
	  		return;
	  	}
	  	postDataByPage($(this).text());
	  });
	  
	  // 选择供应商, "删除"按钮
	  $(document).on("click", ".js-delete", function() {
	  	var i = $(this).parents("tr").index();
	  	arrSelect.splice(arrSelect[i], 1);
	  	changeDomOfgysList();
	  });

        function GetQueryString(name)
        {
            var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if(r!=null)return  unescape(r[2]); return null;
        }	  
	  
	  // 提交/ 暂存
	  var lockSubmit = false;
	  $(".commit").click(function(e) {
			e.preventDefault();
			if(lockSubmit){
                return false;
            }
            lockSubmit =true;
			// 表单验证
			var valid = zdy.validate.valid({
				rules:{
					projectid:{required:true},
					name:{required:true},
					timelimit:{required:true},
					time:{required:true},
					district:{required:true},
					bzmoney:{required:true},
					contacts:{required:true},
					phone:{required:true, isMobile:true}
				}, message:{
					projectid:{required:"项目编号不能为空"},
					name:{required:"请填写项目名称"},
					timelimit:{required:"请填写有效时间"},
					time:{required:"请选择发布时间"},
					district:{required:"请选择所在地区"},
					bzmoney:{required:"请选择需要交纳保证金"},
					contacts:{required:"请填写联系人"},
					phone:{required:"请填写联系电话", isMobile:"请填写有效的11位手机号。"}
				}	
			});
			if (!valid) {
				lockSubmit = false;
				return;
			}
			
			// 数据上传			
	  	var formData = new FormData($("#submitForm").get(0));
	  	var action = $(this).val();
			var address = $("#province").val() + $("#city").val() + $("#district").val();

			for (var i = 0; i < arrSelect.length; i++) {
				supplierInfo.push(arrSelect[i].id);
			}
			var synopsis = ue.getContent();
      var onId = GetQueryString('id');	
				
      formData.append("action",action);
			formData.append("address", address);
			formData.append("supplierInfo", JSON.stringify(supplierInfo));
			formData.append("synopsis", synopsis);
			formData.append("id", onId);	
			var needData = formData.get("needData");
			if (!needData) {
				formData.append("needData",needData);
			}		
			
			$.ajax({
				type:"post",
				url:"/admin/bidding/edit",
				async:true,
				data:formData,
        processData: false,
        contentType: false,
        dataType: "json",
				success: function(data) {
					console.log("成功");
					window.location.href = "/admin/bidding/index?page=1";
				}
			});	  
	  });	  	
	});
</script>

</body>

</html>